//
//  FMCardView.m
//  FreeMark
//
//  Created by gaopr on 2019/2/17.
//  Copyright © 2019年 gaopr. All rights reserved.
//

#import "FMCardView.h"
#import "UIConfig.h"

@interface FMCardView ()<UITextViewDelegate,UITextFieldDelegate>



@end

@implementation FMCardView

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor whiteColor];
        self.layer.cornerRadius = 3;
        self.clipsToBounds = YES;
        
        UIImageView *bgImageView = [[UIImageView alloc] init];
        bgImageView.contentMode = UIViewContentModeScaleAspectFill;
        bgImageView.backgroundColor = [UIColor clearColor];
        [self addSubview:bgImageView];
        [bgImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self);
        }];
        self.bgImageView = bgImageView;
        
        UILabel *titleLabel = [[UILabel alloc] init];
        titleLabel.font = [UIFont systemFontOfSize:20];
        titleLabel.textColor = [UIColor blackColor];
        titleLabel.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.4];
        titleLabel.text = @"标题";
        titleLabel.layer.cornerRadius = 3;
        titleLabel.clipsToBounds = YES;
        [titleLabel sizeToFit];
        [self addSubview:titleLabel];
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.mas_left).offset(kFMargin);
            make.top.mas_equalTo(self.mas_top).offset(kFMargin);
        }];
        self.titleLabel = titleLabel;
        
        UITextField *txtField = [[UITextField alloc] init];
        txtField.delegate = self;
        txtField.textAlignment = NSTextAlignmentCenter;
        txtField.textColor = titleLabel.textColor;
        txtField.font = titleLabel.font;
        txtField.placeholder = @"请输入标题";
        [self addSubview:txtField];
        [txtField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(titleLabel);
            make.top.mas_equalTo(titleLabel.mas_bottom).offset(kFMargin);
            make.right.mas_equalTo(self.mas_right).offset(-kFMargin);
        }];
        self.titleTextField = txtField;
        
        UIButton *titleColorButton = [self colorButtonWithBackGroundColor:txtField.textColor];
        [self addSubview:titleColorButton];
        titleColorButton.backgroundColor = txtField.textColor;
        CGFloat btnH = titleLabel.frame.size.height;
        [titleColorButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(titleLabel.mas_right).offset(kFMargin);
            make.centerY.equalTo(titleLabel);
            make.height.mas_equalTo(btnH);
            make.width.mas_equalTo(btnH / 0.618);
        }];
        self.titleColorButton = titleColorButton;
        
        UILabel *descLabel = [[UILabel alloc] init];
        descLabel.layer.cornerRadius = 3;
        descLabel.clipsToBounds = YES;
        descLabel.font = [UIFont systemFontOfSize:20];
        descLabel.textColor = [UIColor blackColor];
        descLabel.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.4];
        descLabel.text = @"简述";
        [descLabel sizeToFit];
        [self addSubview:descLabel];
        [descLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.mas_left).offset(kFMargin);
            make.top.mas_equalTo(txtField.mas_bottom).offset(kFMargin);
        }];
        self.descLabel = descLabel;
        
        UIButton *descLabelColorButton = [self colorButtonWithBackGroundColor:descLabel.textColor];
        [self addSubview:descLabelColorButton];
        btnH = descLabel.frame.size.height;
        [descLabelColorButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(descLabel.mas_right).offset(kFMargin);
            make.centerY.equalTo(descLabel);
            make.height.mas_equalTo(btnH);
            make.width.mas_equalTo(btnH / 0.618);
        }];
        self.descLabelColorButton = descLabelColorButton;
        
        CGFloat buttonH = 18;
        NSMutableArray *buttons = [[NSMutableArray alloc] init];
        
        UIButton *confirmButton = [self menuButton];
        [confirmButton setTitle:@"确定" forState:UIControlStateNormal];
        [confirmButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateDisabled];
        confirmButton.enabled = NO;
        [self addSubview:confirmButton];
        [buttons addObject:confirmButton];
        self.confirmButton = confirmButton;
        
        UIButton *cancelButton = [self menuButton];
        [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
        [self addSubview:cancelButton];
        [buttons addObject:cancelButton];
        
        MASViewAttribute *leading = self.mas_leading;
        for (UIView *btn in buttons) {
            [btn mas_makeConstraints:^(MASConstraintMaker *make) {
                make.leading.equalTo(leading).offset(kFMargin);
                make.bottom.equalTo(self.mas_bottom).offset(-kFMargin);
            }];
            leading = btn.mas_trailing;
        }
        
        [buttons.lastObject mas_makeConstraints:^(MASConstraintMaker *make) {
            make.trailing.equalTo(self.mas_trailing).offset(-kFMargin);
            make.height.mas_equalTo(buttonH);
            make.height.equalTo(buttons);
            make.top.equalTo(buttons);
            make.width.equalTo(buttons);
        }];
        
        buttons = [[NSMutableArray alloc] init];
        UIButton *backGroundColorButton = [self menuButton];
        [backGroundColorButton setTitle:@"背景颜色" forState:UIControlStateNormal];
        [self addSubview:backGroundColorButton];
        [buttons addObject:backGroundColorButton];
        self.backGroundColorButton = backGroundColorButton;
        
        UIButton *backGroundImageButton = [self menuButton];
        [backGroundImageButton setTitle:@"背景图片" forState:UIControlStateNormal];
        [self addSubview:backGroundImageButton];
        [buttons addObject:backGroundImageButton];
        self.backGroundImageButton = backGroundImageButton;
        
        leading = self.mas_leading;
        for (UIView *btn in buttons) {
            [btn mas_makeConstraints:^(MASConstraintMaker *make) {
                make.leading.equalTo(leading).offset(kFMargin);
                make.bottom.equalTo(confirmButton.mas_top).offset(-kFMargin);
            }];
            leading = btn.mas_trailing;
        }
        
        [buttons.lastObject mas_makeConstraints:^(MASConstraintMaker *make) {
            make.trailing.equalTo(self.mas_trailing).offset(-kFMargin);
            make.height.mas_equalTo(buttonH);
            make.height.equalTo(buttons);
            make.top.equalTo(buttons);
            make.width.equalTo(buttons);
        }];
        
        UITextView *descTextView = [[UITextView alloc] init];
        descTextView.delegate = self;
        descTextView.textColor = descLabel.textColor;
        descTextView.backgroundColor = [UIColor clearColor];
        [self addSubview:descTextView];
        [descTextView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(descLabel);
            make.top.mas_equalTo(descLabel.mas_bottom).offset(kFMargin);
            make.right.mas_equalTo(self.mas_right).offset(-kFMargin);
            make.bottom.mas_equalTo(backGroundColorButton.mas_top).offset(-kFMargin);
        }];
        self.descTextView = descTextView;
        
        UILabel *descTextViewPlaceHolder = [[UILabel alloc] init];
        descTextViewPlaceHolder.text = @"请输入简述";
        descTextViewPlaceHolder.font = descTextView.font;
        descTextViewPlaceHolder.textColor = [descTextView.textColor colorWithAlphaComponent:0.3];
        [self addSubview:descTextViewPlaceHolder];
        [descTextViewPlaceHolder mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(descTextView.mas_left).offset(3);
            make.top.mas_equalTo(descTextView.mas_top).offset(5);
        }];
        self.descTextViewPlaceHolder = descTextViewPlaceHolder;
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFiledTxtDidChanged) name:UITextFieldTextDidChangeNotification object:nil];
    }
    return self;
}

- (void)textFiledTxtDidChanged
{
    self.confirmButton.enabled = self.titleTextField.text.length;
}

-(void)textViewDidChange:(UITextView *)textView
{
    self.descTextViewPlaceHolder.hidden = textView.text.length;
}

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    UIView *view = [super hitTest:point withEvent:event];
    if (view && view != self.titleColorButton && view != self.descLabelColorButton && view != self.backGroundColorButton)
    {
        if ([self.delegate respondsToSelector:@selector(onColorViewHidden:)])
            [self.delegate onColorViewHidden:self];
    }
    
    if (view && view != self.titleTextField && view != self.descTextView)
    {
        if ([self.delegate respondsToSelector:@selector(onColorViewEndEdit:)])
            [self.delegate onColorViewEndEdit:self];
    }
    
    return view;
}

- (UIButton *)menuButton
{
    UIButton *cancelButton = [[UIButton alloc] init];
    cancelButton.layer.cornerRadius = 3;
    cancelButton.clipsToBounds = YES;
    [cancelButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    cancelButton.titleLabel.font = [UIFont systemFontOfSize:8];
    [cancelButton setBackgroundColor:[[UIColor blackColor] colorWithAlphaComponent:0.6]];
    return cancelButton;
}

- (UIButton *)colorButtonWithBackGroundColor:(UIColor *)color
{
    UIButton *titleColorButton = [[UIButton alloc] init];
    [titleColorButton setTitle:@"T-Color" forState:UIControlStateNormal];
    titleColorButton.titleLabel.font = [UIFont systemFontOfSize:8];
    titleColorButton.backgroundColor = color;
    CGFloat red = 0;
    CGFloat green = 0;
    CGFloat blue = 0;
    CGFloat alpha = 0;
    [titleColorButton.backgroundColor getRed:&red green:&green blue:&blue alpha:&alpha];
    [titleColorButton setTitleColor:[UIColor colorWithRed:255 - red green:(255 - green) blue:(255 - blue) alpha:alpha] forState:UIControlStateNormal];
    titleColorButton.layer.cornerRadius = 3;
    titleColorButton.clipsToBounds = YES;
    return titleColorButton;
}

@end
